package priv.zgh.forum.check.service.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import priv.zgh.forum.check.service.EmailService;
import priv.zgh.forum.exception.ForumException;

import javax.mail.internet.MimeMessage;

@Service
public class EmailServiceImp implements EmailService{

    JavaMailSenderImpl mailSender;
    @Autowired
    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public void sendEmail(String email, String content){
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            helper.setSubject("验证码");
            helper.setText(content);

            helper.setTo(email);
            helper.setFrom("1376705547@qq.com");

            mailSender.send(mimeMessage);
        }catch (Exception e){
            throw new ForumException(20001,"发送邮件失败!");
        }
    }


}
